
// 新增方法：更新 UI 布局
public void updateUILayout(int rows, int cols, int mines)
{
    this.rows = rows;
    this.cols = cols;

    // 清除现有的按钮和监听器
    if (buttons != null) {
        for (int x = 0; x < buttons.length; x++) {
            if (buttons[x] != null) {
                for (int y = 0; y < buttons[x].length; y++) {
                    if (buttons[x][y] != null) {
                        buttons[x][y].removeMouseListener(null);
                        buttons[x][y].removeActionListener(null);
                        buttons[x][y] = null;
                    }
                }
            }
        }
    }

    // 重新创建按钮数组
    buttons = new JButton[cols][rows];
    for (int y = 0; y < rows; y++) {
        for (int x = 0; x < cols; x++) {
            buttons[x][y] = new JButton("");
            buttons[x][y].setName(x + "," + y);
            buttons[x][y].setFont(new Font("Serif", Font.BOLD, 24));
            buttons[x][y].setBorder(BorderFactory.createLineBorder(Color.black, 1, true));
        }
    }

    // 构建新的游戏面板
    JPanel gameBoard = new JPanel();
    gameBoard.setLayout(new GridLayout(rows, cols, 0, 0));
    for (int y = 0; y < rows; y++) {
        for (int x = 0; x < cols; x++) {
            gameBoard.add(buttons[x][y]);
        }
    }

    // 替换旧的游戏面板
    Component[] components = getContentPane().getComponents();
    for (Component component : components) {
        if (component instanceof JPanel) {
            remove(component);
        }
    }
    add(gameBoard, BorderLayout.CENTER);
    revalidate();
    repaint();
    
    // 更新地雷数量显示
    setMines(mines);
}
